We will use the get_acs() function from tidycensus package to pull
data from the census bureau. The function defaults to the 2017-2021
5-year ACS dataset. Which is necessary for us, since there are a list of
counties in Oregon with a population of less than 65,000.
# Data objects needed
or_grads <- get_acs(
geography = "county",
variables = "DP02_0066P",
state = "OR",
year = 2021,
)
or_grads_table <- or_grads %>%
separate(NAME, into = c("county", "state"), sep = ", ") %>%
arrange(-estimate) %>%
select(county, estimate, moe)
Bottom 3 counties –
or_plot <- ggplot(or_grads_table, aes(x = estimate,
y = reorder(county, estimate))) +
geom_point(color="navy", size = 3) +
scale_x_continuous(labels = function(x) paste0(x, '%')) +
scale_y_discrete(labels = function(x) str_remove(x, " County")) +
labs(title = "% Population with graduate degrees, 2017-2021 ACS",
subtitle = "Counties in Oregon",
caption = "Data acquired with R and tidycensus",
x = "ACS estimate",
y = "") +
theme_minimal(base_size = 12)
or_plot

or_plot_errorbar <- ggplot(or_grads, aes(x = estimate, y = reorder(NAME, estimate))) +
geom_errorbar(aes(xmin = estimate - moe, xmax = estimate + moe),
width = 0.5, linewidth = 0.5) +
geom_point(color = "gold", size = 3) +
scale_x_continuous(labels = function(x) paste0(x, '%')) +
scale_y_discrete(labels = function(x) str_remove(x, " County, Oregon|, Oregon")) +
labs(title = "% Population with graduate degrees, 2017-2021 ACS",
subtitle = "Counties in Oregon",
caption = "Data acquired with R and tidycensus. Error bars represent margin of error around estimates.",
"ACS estimate",
y = "",
x = "Percentage") +
theme_minimal(base_size = 12)
or_plot_errorbar

ggplotly(or_plot_errorbar, tooltip = "x")
NA
# Data objects needed
or_grads_geom <- get_acs(
geography = "county",
variables = "DP02_0066P",
state = "OR",
year = 2021,
geometry = TRUE
)
ggplot() + # use ggplot to create the plot.
geom_sf(data = or_grads_geom, # use geom_sf to plot the polyline geometry.
aes(fill = as.factor(estimate)),
linewidth = 1) + # color based on year.
scale_color_discrete(name = "estimate") + # set color scale
coord_sf(datum = NA) + # put geometries on the same coordinate reference system
theme_void()